<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *售后列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'用户违纪'),
	array('op'=>'class','title'=>'导出违纪表'),
	array('op'=>'log','title'=>'违纪记录'),
);
//售后列表
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('fromstoreid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['classid']){
		$params['classid']=intval($_RQ['classid']);
		$return['params']['classid']=$_RQ['classid'];
	}
	if(!empty($_RQ['title'])){
		$params2=array('fromstoreid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
		$params2['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
		$xueshengids=$this->getall('xuesheng',$params2,array('id','title'),'id');
		if($xueshengids){
			$xueshengids=array_keys($xueshengids);
		}else{
			$xueshengids=-1;
		}
		$params['xueshengid']=$xueshengids;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$list=pdo_getslice('jy_yxw_xuesheng',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$day=date('Y-m-d',time());
		foreach($list as &$item){
			$item['member']=$this->model('xuesheng')->member_show($item['id']);
			$item['wj1']=pdo_count('jy_yxw_weiji_log',array('type'=>1,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['wj2']=pdo_count('jy_yxw_weiji_log',array('type'=>2,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['wj3']=pdo_count('jy_yxw_weiji_log',array('type'=>3,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、这里显示本门店的售后！
		','del_message'=>'确认删除该售后吗？','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'sn','title'=>'编号','type'=>'string'),
			array('name'=>'classid','title'=>'售后群','type'=>'select','options'=>'class'),
			array('type'=>'string','title'=>'普通违纪次数','name'=>'wj1'),
			array('type'=>'string','title'=>'中等违纪次数','name'=>'wj2'),
			array('type'=>'string','title'=>'严重违纪次数','name'=>'wj3'),
		),'action'=>array(
			array('type'=>'modal','action'=>'jy_yxw/xuesheng.weiji/settype','title'=>'添加违纪'),
			array('type'=>'href','action'=>'jy_yxw/xuesheng.weiji/log/','title'=>'查看违纪'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'售后名称','name'=>'title','help'=>''),
			array('name'=>'classid','width'=>'15','title'=>'售后群','type'=>'select','options'=>'class'),
			//array('name'=>'storeid','width'=>'15','title'=>'所属门店','type'=>'select','options'=>'stores'),
		),
	);
	$return['type']=$this->weiji_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='settype'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		if(!$post['type']){
			exi('请选择违纪类型','error');
		}
		if(!$post['remark']){
			exi('请填写违纪原因','error');
		}
		$this->model('uplog')->upkaoqing_log($post);
		exi('操作成功！','','//list/');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$item['xueshengid']=$item['id'];
		unset($item['id']);
	}else{
		exi('请先选择售后','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'添加违纪',
	);
	$htmls['edit']=array(
		array('type'=>'radio','title'=>'违纪类型','name'=>'type','options'=>'type'),
		array('type'=>'text','title'=>'违纪原因','name'=>'remark'),
	);
	$return['type']=$this->weiji_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='log'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']['status']=$_RQ['status'];
	if($_RQ['type']){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	if($_RQ['id']){
		$params['xueshengid']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$list=pdo_getslice('jy_yxw_weiji_log',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('xuesheng')->member_show($item['xueshengid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、这里显示违纪资料！
		','del_message'=>'确认删除该违纪吗？','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'type','title'=>'违纪类型','type'=>'radio','options'=>'weiji_type'),
			array('type'=>'string','title'=>'违纪原因','name'=>'remark'),
			array('name'=>'createtime','title'=>'日期','type'=>'time'),
		),'action'=>array(
			//array('type'=>'model','op'=>'setstore','title'=>'修改门店'),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'售后姓名','name'=>'title','help'=>''),
			array('name'=>'type','width'=>'15','title'=>'违纪类型','type'=>'radio','options'=>'weiji_type'),
		),
	);
	$return['workers']=$this->getall('worker',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));	
	$return['weiji_type']=$this->weiji_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='class'){
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
    $orderby='id desc';
	$total='';
	$return['list']=$this->getall('class',$params,array(),'',$orderby);
	foreach($return['list'] as &$item){
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>这里显示本门店的售后群；
		','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'售后群名称','type'=>'string'),
			array('name'=>'workerid','title'=>'售后主管','type'=>'select','options'=>'workers'),
			array('name'=>'kechengid','title'=>'绑定使用','type'=>'select','options'=>'kechengs'),
			array('name'=>'keshi','title'=>'课时','type'=>'string'),
			array('name'=>'xueshengnum','title'=>'用户人数','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'open_time','title'=>'开班时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','action'=>'/xuesheng.kaoqing/export/','title'=>'选择月份导出'),
			array('type'=>'export','action'=>'/xuesheng.weiji/export1/','title'=>'导出上月违纪'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'售后群名称','name'=>'title','help'=>''),
			array('name'=>'status','width'=>'15','title'=>'状态','type'=>'radio','options'=>'status'),
		),
	);
	//$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['workers']=$this->getall('worker',array('role'=>11,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['kechengs']=$this->getall('kecheng',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['status']=$this->class_status;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择售后群','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'按月导出违纪',
	);
	$htmls['edit']=array(
		array('name'=>'title','title'=>'售后群','type'=>'copy'),
		array('name'=>'year','title'=>'选择年份','type'=>'select','options'=>'year'),
		array('name'=>'month','title'=>'选择月份','type'=>'select','options'=>'month'),
	);
	$year=intval(date('Y',time()));
	$return['year']=array('0'=>array('id'=>$year-9,'title'=>$year-9),'1'=>array('id'=>$year-8,'title'=>$year-8),'2'=>array('id'=>$year-7,'title'=>$year-7),'3'=>array('id'=>$year-6,'title'=>$year-6),'4'=>array('id'=>$year-5,'title'=>$year-5),'5'=>array('id'=>$year-4,'title'=>$year-4),'6'=>array('id'=>$year-3,'title'=>$year-3),'7'=>array('id'=>$year-2,'title'=>$year-2),'8'=>array('id'=>$year-1,'title'=>$year-1),'9'=>array('id'=>$year,'title'=>$year));
	$return['month']=array('0'=>array('id'=>1,'title'=>1),'1'=>array('id'=>2,'title'=>2),'2'=>array('id'=>3,'title'=>3),'3'=>array('id'=>4,'title'=>4),'4'=>array('id'=>5,'title'=>5),'5'=>array('id'=>6,'title'=>6),'6'=>array('id'=>7,'title'=>7),'7'=>array('id'=>8,'title'=>8),'8'=>array('id'=>9,'title'=>9),'9'=>array('id'=>10,'title'=>10),'10'=>array('id'=>11,'title'=>11),'11'=>array('id'=>12,'title'=>12));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	if(!$post['year']){
		exi('请选择年份！','error');
	}
	if(!$post['month']){
		exi('请选择月份！','error');
	}
	$lasttime=strtotime($post['year'].'-'.$post['month'].'-01');
	$url=download_url('jy_yxw/xuesheng.weiji/export1/id='.$post['id'].'&lasttime='.$lasttime);
	exi('点确定开始下载','','',$url);
}
if($_RQ['op']=='export1'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['id']){
		$params['classid']=intval($_RQ['id']);
	}else{
		exi('请选择售后群！','error');
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	$lasttime=$_RQ['lasttime']?:strtotime('-1 month');
	$year =date('Y',$lasttime);
	$month =date('m',$lasttime);
	$tstart=strtotime($year.'-'.$month);
	$month +=1;
	$endtime=strtotime($year.'-'.$month);
	$params['createtime between']=array($tstart,$endtime);
	$list=pdo_getall('jy_yxw_weiji_log',$params,$fields);
	if($list){
		foreach($list as &$item){
			$item['title']=pdo_getcolumn('jy_yxw_xuesheng',array('id'=>$item['xueshengid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$header=array(
		'xueshengid'=>'售后ID',
		'title'=>'售后姓名',
		'remark'=>'违纪描述',
		'createtime'=>'违纪时间',
	);
	$classtitle=pdo_getcolumn('jy_yxw_class',array('id'=>$params['classid'],'uniacid'=>$_SESSION['uniacid']),'title');
	model('core/tools')->export2excel($header,$list,$classtitle.'['.$year . '-' . $month . ']违纪');
}